<template>
	<view class="top">
		<i class="iconfont icon-geren2"></i>
		<text>{{personForm.Name}}</text>
		<i class="iconfont icon-a-Frame133"></i>
		<text>{{personForm.CardNo}}</text>
		<i v-if="personForm.Sex=='男'" class="iconfont icon-sex_man"></i>
		<i v-else class="iconfont icon-sex_woman"></i>
		<text>{{personForm.Sex}}</text>
		<view class="Vtext">
			<text v-show="Glbz.includes('高血压')">高</text>
			<text v-show="Glbz.includes('糖尿病')" class="Vtext2">糖</text>
			<text v-show="Glbz.includes('精神障碍')" class="Vtext3">精</text>
		</view>
	</view>
	<view class="content">
		<view>
			<text class="topText">个人档案</text>
		</view>
		<hr />
		<view class="buttonView uni-mt-5">
			<button @click="goHealth()"><text>基本信息</text><i class="iconfont icon-xinxiguanli"></i></button>
		</view>
		<view class="uni-mt-5">
			<text class="topText">时间查询</text>
		</view>
		<hr />
		<view class="example-body uni-mt-5">
			<uni-datetime-picker v-model="range" type="daterange" @change="maskClick" />
		</view>
		<view>
			<view class="topText sugar">
				<text @click="sugarChang(1)" :class="msg=='1'?'active':''" v-show="Glbz.includes('高血压')">高血压</text>
				<text @click="sugarChang(2)" :class="msg=='2'?'active2':''" v-show="Glbz.includes('糖尿病')">糖尿病</text>
				<text @click="sugarChang(3)" :class="msg=='3'?'active2':''" v-show="Glbz.includes('精神障碍')">精神障碍</text>
				<text class="three"></text>
			</view>
		</view>
		<view class="dib-box" v-for="item in tableData" :key="item.Id">
			<view class="left">
				<view class="imgView">
					<i class="iconfont icon-geren1" v-show="!item.picture"></i>
					<img v-show="item.picture" :src="item.picture" alt="" srcset="" />
				</view>
				<view class="imgText">
					<button @click="chooseImage(item)"><i class="iconfont icon-paizhao-shi"></i>拍照</button>
				</view>
			</view>
			<view class="right">
				<view class="one">
					<text>
						<i class="iconfont icon-xinxiliebiao"></i>
						{{item.SuifangRiqi}}
					</text>
					<text class="lastText">
						<i class="iconfont icon-shuangjiantouyou"></i>
						{{ item.Xiacisuifangriqi }}
					</text>
				</view>
				<view class="two" v-if="msg=='1' || msg=='2'">
					<text>血压:{{item.TizhengXueyaSsy}}/{{item.TizhengXueyaSzy}}</text>
					<text>BMI:{{item.TizhengBmi1}}</text>
					<text class="lastText">随访分类:{{SuiFangFenLei[item.SuiFangFenLei-1]?.text }}</text>
				</view>
				<view class="two" v-if="msg=='3'">
					<text>随访方式:{{ SuifangFangshi[item.SuifangFangshi-1]?.text}}</text>
					<text>危险评估性:{{ PingGu[item.WeiXianXingPingGu]?.text}}</text>
				</view>
				<view class="three">
					<text>
						<i class="iconfont icon-geren"></i>
						测试医生:{{ item.Suifangyisheng}}
					</text>
					<text class="lastText">下步措施:</text>
				</view>
				<view class="four">
					<button class="ben" @click="updataToeditBlood(item)"><i
							class="iconfont icon-huabi"></i>修改本次随访</button>
					<button class="lastText" @click="ToeditBlood()"><i class="iconfont icon-jiahao"></i>下次随访</button>
				</view>
			</view>
		</view>
		<view class="uni-mt-10 uni-mb-10 addBut">
			<button class="ben" @click="ToeditBlood()"><i
					class="iconfont icon-jiahao1"></i>新增{{ msg=="1"? '高血压' :msg=='2' ? '糖尿病' : msg =='3' ? '精神障碍' :'' }}随访</button>
		</view>
		<!-- 提示窗示例 -->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="否" confirmText="是" title="通知" :content="content"
				@confirm="dialogConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getDetails,
		addPerson,
		getSuifangList,
		getMessage,
		getTNBMessage,
		getTNBList,
		getJSZAList,
		getJSZAMessage,
		getSuifang
	} from '@/api/index.js'
	import {
		pathToBase64
	} from 'image-tools'
	export default {
		data() {
			return {
				title: 'Hello',
				msg: '1',
				Glbz: '',
				tableData: [],
				personForm: {},
				content: '',
				SuiFangFenLei: [{
					text: '控制满意',
					value: '1'
				}, {
					text: '控制不满意',
					value: '2'
				}, {
					text: '不良反应',
					value: '3'
				}, {
					text: '并发症',
					value: '4'
				}],
				SuifangFangshi: [{
					text: '门诊',
					value: '1'
				}, {
					text: '家庭',
					value: '2'
				}, {
					text: '电话',
					value: '3'
				}],
				PingGu: [{
					text: '0级',
					value: '0'
				}, {
					text: '1级',
					value: '1'
				}, {
					text: '2级',
					value: '2'
				}, {
					text: '3级',
					value: '3'
				}, {
					text: '4级',
					value: '4'
				}, {
					text: '5级',
					value: '5'
				}],
				timeData: {
					StartDate: '',
					EndDate: '',
					appID: ''
				},
				Id: '', // 存储点击当前的Id
				dataForm: {}, // 存储基本信息
				imgShow: '',
				resForm: {},
				range: []
			}
		},
		onLoad(option) {
			this.dataForm = JSON.parse(option.data)
			this.getTime()
			this.timeData.appID = JSON.parse(option.data).Id
			this.Glbz = JSON.parse(option.data).Glbz
			this.personForm.Name = JSON.parse(option.data).Name
			this.personForm.CardNo = JSON.parse(option.data).CardNo
			this.personForm.Sex = JSON.parse(option.data).Sex
			this.resForm.Quyu = JSON.parse(option.data).Quyu
			this.showList()

		},
		onShow() {
			if (this.Glbz.includes('高血压')) {
				this.msg = '1'
			} else if (this.Glbz.includes('糖尿病')) {
				this.msg = '2'
			} else if (this.Glbz.includes('精神障碍')) {
				this.msg = '3'
			}
			this.timeData.appID ? this.showList() : ''
		},
		methods: {
			maskClick(e){
				this.showList()
			},
			goHealth() {
				uni.navigateTo({
					url: `/pages/health/health?data=${JSON.stringify(this.dataForm)}`
				})
			},
			chooseImage(val) {
				var _this = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图original还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择、摄像头
					success: function(res) {
						let tempFilePath = res.tempFilePaths[0]
						_this.imgToBase64(tempFilePath)
							.then(base64String => {
								_this.resForm.picture = base64String
								_this.resForm.Id = val.AppID
								addPerson(_this.resForm).then(res => {
									_this.showList()
								})
							})
							.catch(error => console.error('转换出错:', error));
					}
				})
			},
			imgToBase64(data) {
				return new Promise((resolve, reject) => {
					pathToBase64(data).then(base64 => {
						resolve(base64)
					}).catch(error => {
						console.error(error)
						reject(error)
					})
				})
			},
			getTime() {
				// 创建一个表示今天的Date对象
				var today = new Date();
				// 设置为去年的同一天
				today.setFullYear(today.getFullYear() - 1);
				// 格式化日期字符串（YYYY-MM-DD）
				function formatDate(date) {
					var year = date.getFullYear().toString();
					var month = (date.getMonth() + 1).toString().padStart(2, '0'); // 注意月份从0开始，所以要加上1
					var day = date.getDate().toString().padStart(2, '0');
					return `${year}-${month}-${day}`;
				}
				// 获取今天的年月日
				this.range[0] = formatDate(today) + ' 00:00:00';
				this.range[1] = formatDate(new Date()) + ' 23:59:59';
			},
			showList() {
				this.timeData.StartDate = this.range[0] ;
				this.timeData.EndDate = this.range[1];
				if (this.msg == '1') {
					getSuifangList(this.timeData).then(res => { // 高血压随访列表
						res.forEach((item, index) => {
							res[index].SuifangRiqi = res[index].SuifangRiqi?.split('T')[0].toString()
							res[index].Xiacisuifangriqi = res[index].Xiacisuifangriqi?.split('T')[0]
								.toString()
						})
						this.tableData = res
					})
				} else if (this.msg == '2') { // 糖尿病随访列表
					getTNBList(this.timeData).then(res => {
						res.forEach((item, index) => {
							res[index].SuifangRiqi = res[index].SuifangRiqi?.split('T')[0].toString()
							res[index].Xiacisuifangriqi = res[index].Xiacisuifangriqi?.split('T')[0]
								.toString()
						})
						this.tableData = res
					})
				} else if (this.msg == '3') {
					getJSZAList(this.timeData).then(res => {
						res.forEach((item, index) => {
							res[index].SuifangRiqi = res[index].SuifangRiqi?.split('T')[0].toString()
							res[index].Xiacisuifangriqi = res[index].Xiacisuifangriqi?.split('T')[0]
								.toString()
						})
						this.tableData = res
					})
				}
			},
			sugarChang(val) {
				this.msg = val
				this.showList()
			},
			updataToeditBlood(val) {
				this.Id = val.Id
				this.dialogConfirm()
			},
			ToeditBlood() { // 新增随访只传appID
				this.Id = ""
				if (this.msg == '1') { // 高血压
					getMessage(this.timeData.appID).then(res => {
						if (res.code == "200") {
							this.content = res.message
							this.dialogToggle('success')
						} else if (res.code == 1) {
							uni.navigateTo({
								url: `/pages/editBlood/editBlood?appId=${this.timeData.appID}&Id=${this.Id}&msg=${this.msg}`
							})
						}
					})
				} else if (this.msg == '2') { // 糖尿病
					getTNBMessage(this.timeData.appID).then(res => {
						if (res.code == "200") {
							this.content = res.message
							this.dialogToggle('success')
						} else if (res.code == 1) {
							uni.navigateTo({
								url: `/pages/TNBBlood/TNBBlood?appId=${this.timeData.appID}&Id=${this.Id}&msg=${this.msg}`
							})
						}
					})
				} else if (this.msg == '3') { // 精神障碍
					getJSZAMessage(this.timeData.appID).then(res => {
						if (res.code == "200") {
							this.content = res.message
							this.dialogToggle('success')
						} else if (res.code == 1) {
							uni.navigateTo({
								url: `/pages/JSZABlood/JSZABlood?appId=${this.timeData.appID}&Id=${this.Id}&msg=${this.msg}`
							})
						}
					})
				}

			},
			dialogConfirm() {
				if (this.msg == '1') { // 高血压
					uni.navigateTo({
						url: `/pages/editBlood/editBlood?appId=${this.timeData.appID}&Id=${this.Id}&msg=${this.msg}`
					})
				} else if (this.msg == '2') { // 糖尿病
					uni.navigateTo({
						url: `/pages/TNBBlood/TNBBlood?appId=${this.timeData.appID}&Id=${this.Id}&msg=${this.msg}`
					})
				} else if (this.msg == '3') { // 精神障碍
					uni.navigateTo({
						url: `/pages/JSZABlood/JSZABlood?appId=${this.timeData.appID}&Id=${this.Id}&msg=${this.msg}`
					})
				}

			},
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
		}
	}
</script>

<style lang="scss">
	.content {
		width: 100wh;
		padding: 0 15rpx;
		box-sizing: border-box;
	}

	.top {
		display: flex;
		font-size: 22rpx;
		background-color: #C9F7CA;
		padding: 0 15rpx;
		height: 60rpx;
		line-height: 60rpx;

		text {
			margin-right: 10rpx;
		}

		.Vtext {
			text {
				background-color: #D54D31;
				color: white;
				padding: 5rpx 10rpx;
				border-radius: 100rpx;
			}

			.Vtext2 {
				background-color: #EA44E6;
			}

			.Vtext3 {
				background-color: #F9BB00;
			}
		}
	}

	.topText {
		display: block;
		font-size: 26rpx;
		margin: 10px 0;
	}

	hr {
		background-color: #dfdfdf;
		border: 1px solid #dfdfdf;
		border-bottom: none;
	}

	.sugar {
		width: 100%;
		height: 60rpx;
		display: flex;
		font-size: 26rpx;

		text {
			width: 30%;
			height: 60rpx;
			line-height: 30px;
			text-align: center;
			border-bottom: 1px solid #dfdfdf;
		}

		.three {
			height: 60rpx;
			width: 100%;
			border-bottom: 1px solid #dfdfdf;
		}

		.active {
			border-bottom: none;
			border-top: 1px solid #dfdfdf;
			border-right: 1px solid #dfdfdf;
		}

		.active2 {
			border: 1px solid #dfdfdf;
			border-bottom: none;
		}
	}

	.buttonView {
		width: 100%;

		button {
			font-size: 26rpx;
			width: 40%;
			height: 70rpx;
			border: none;
			overflow: hidden;
			background-color: #FF7500;
			color: white;
			margin-left: 0;
			border-radius: 1px !important;
		}

		i {
			padding-left: 20rpx;
			font-size: 30rpx;
		}
	}

	.dib-box {
		display: flex;
		border-bottom: 1px solid #dfdfdf;
		padding: 15rpx 0;
		font-size: 22rpx;

		.left {
			width: 23%;

			.imgView {
				overflow: hidden;
				background-color: #CFCFCF;
				height: 180rpx;

				i {
					display: inline-block;
					color: #fff;
					margin-top: 15rpx;
					font-size: 150rpx;
				}

				;

				img {
					background-size: cover;
					width: 100%;
					// height: 100%;
				}
			}

			.imgText {
				button {
					display: flex;
					margin-top: 15rpx;
					width: 100%;
					height: 60rpx;
					align-items: center;
					background-color: #fff;
					font-size: 22rpx;

					i {
						color: #7e7e7e;
						font-size: 38rpx;
						margin: 0;
					}
				}
			}
		}

		.right {
			width: 75%;
			font-size: 26rpx;
			margin-left: 15rpx;

			.one {
				font-weight: bold;
				display: flex;
				justify-content: space-between;
				margin-bottom: 35rpx;

				.lastText {
					font-weight: 500;

					i {
						font-size: 26rpx
					}
				}
			}

			.two,
			.three {
				display: flex;
				margin: 26rpx 0;

				text {
					margin-right: 10rpx;

					.lastText {
						margin-right: 0;
					}
				}

				i {
					font-size: 28rpx
				}
			}

			.four {
				width: 75%;
				display: flex;

				button {
					border-radius: none;
					font-size: 22rpx;
					padding: 0 15rpx;
					height: 60rpx;
					background-color: #fff;
					display: flex;
					align-items: center;
				}

				i {
					font-size: 25rpx
				}

				.ben {
					background-color: #00961C;
					color: #fff;
				}
			}
		}
	}

	.addBut {
		width: 40%;

		button {
			font-size: 22rpx;
			height: 60rpx;
			line-height: 60rpx;
			background-color: #00961C;
			color: #fff;
		}
	}
</style>